import * as Webpack from "webpack";
import * as HtmlWebpackPlugin from "html-webpack-plugin";
import * as ExtractTextPlugin from "extract-text-webpack-plugin";
import AppConfig from "./app.config";

const ENV = {
    "process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV || "development"),
    "__DEV__": AppConfig.dev
};

const htmlConfig: HtmlWebpackPlugin.Config = {
    favicon: AppConfig.favicon,
    filename: AppConfig.indexName,
    template: AppConfig.indexHtml,
    hash: true,
    showErrors: true,
    minify: AppConfig.dev ? false : {
        removeComments: true,
        collapseWhitespace: true,
        removeRedundantAttributes: true,
        useShortDoctype: true,
        removeEmptyAttributes: true,
        removeStyleLinkTypeAttributes: true,
        keepClosingSlash: true,
        minifyJS: true,
        minifyCSS: true,
        minifyURLs: true
    }
};

const buildConfig = () => {
    const config: Webpack.Plugin[] = [
        new Webpack.DefinePlugin(ENV),
        new HtmlWebpackPlugin(htmlConfig)
    ];
    if (AppConfig.dev) {
        config.push(
            new Webpack.NamedModulesPlugin(),
            new Webpack.HotModuleReplacementPlugin()
        );
    } else {
        config.push(
            new Webpack.HashedModuleIdsPlugin(),
            new Webpack.optimize.UglifyJsPlugin({
                compress: {
                    warnings: false,
                    comparisons: false,
                },
                sourceMap: false,
            })
        );
    }
    config.push(
        new ExtractTextPlugin({
            filename: "assets/css/main.[hash:8].css"
        })
    );
    return config;
};
export default buildConfig();
